#include "NFileSystem.h"

#include "CiniReader.h"
#include "CiniWriter.h"
#include "NEngineException.h"

#include <stdio.h>

#include "NeroCore\NMemoryManager.h"
#include "NeroCore\NLogger.h"
#include "NeroCore\NFile.h"

#pragma comment(lib,"SimpleINI.lib")

using namespace Engine;
using namespace Core;

#define MAX_PATH 260
const char*  INI_FILE_PATH = "..\\..\\assets\\ini\\";

NFileSystem::NFileSystem(void)
{
}

NFileSystem::~NFileSystem(void)
{
}

void NFileSystem::Initialize()
{
}

void NFileSystem::Load(const char*)
{

}

void NFileSystem::FileLoad(char ** buffer , const char* fileName)
{
	NFile aFile;

	FILE* file = aFile.GetFile();
	FileOpen(&file, fileName);
	
	if(file != NULL)
	{
		fseek(file, 0, SEEK_END);
		long lSize = ftell(file);
		rewind(file);
	
		*buffer = MY_NEW(3,"FileLoad::FileLoad:Buffer") char[lSize+1];

		fread (*buffer, 1, lSize, file);

		(*buffer)[lSize] = NULL;

		FileClose(file);
	}
}

void NFileSystem::FileOpen(FILE** file,const char* fileName)
{
	if(strlen(fileName))
	{
		try
		{
			if(fopen_s(file, fileName, "rb") != 0)
			{
				throw Engine::NEngineException("File Failed To Open...");
			}
		}
		catch(Engine::NEngineException ex)
		{
			std::string error = std::string(ex.what()) + fileName;
			OUTLOG(error.c_str());
		}
	}
}

void NFileSystem::FileClose(FILE* file)
{
	if(file != NULL)
	{
		fclose(file);
	}
}